package controllers

import (
	"encoding/json"
	_ "gitchina/kly_service/errors"
	"gitchina/kly_service/models"
	_ "gitchina/kly_service/util"
	"github.com/astaxie/beego"
)

type WalletController struct {
	beego.Controller
}

// @router /104001 [post]
func (ctrl *WalletController) CreateWallet() {
	var w models.Wallet
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &w); err == nil {
		if err := w.Create(models.ID(ctrl.Ctx.Input.GetData("mbrId").(string))); err == nil {
			ctrl.Data["json"] = models.NewOkResponse(nil)
		} else {
			ctrl.Data["json"] = models.NewFailResponse(err)
		}
		ctrl.ServeJSON()
	} else {
		ctrl.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /104002 [post]
func (ctrl *WalletController) GetBalance() {
	w := &models.Wallet{}
	if balance, err := w.GetBalance(models.ID(ctrl.Ctx.Input.GetData("mbrId").(string))); err == nil {
		ctrl.Data["json"] = models.NewOkResponse(balance)
	} else {
		ctrl.Data["json"] = models.NewFailResponse(err)
	}
	ctrl.ServeJSON()
}

// @router /104003 [post]
func (ctrl *WalletController) ChangePwd() {
	var w models.Wallet
	var rq models.ChangeWalletPayPwd
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &rq); err == nil {
		if err := w.ChangePwd(models.ID(ctrl.Ctx.Input.GetData("mbrId").(string)), rq.OldPayPwd, rq.NewPayPwd); err == nil {
			ctrl.Data["json"] = models.NewOkResponse(nil)
		} else {
			ctrl.Data["json"] = models.NewFailResponse(err)
		}
		ctrl.ServeJSON()
	} else {
		ctrl.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /104004 [post]
func (ctrl *WalletController) Withdraw() {
	var w models.Wallet
	var rq models.WithdrawRQ
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &rq); err == nil {
		memberId := models.ID(ctrl.Ctx.Input.GetData("mbrId").(string))
		if err := w.Withdraw(memberId, rq.CardId, rq.PayPwd, rq.Amount); err == nil {
			ctrl.Data["json"] = models.NewOkResponse(nil)
		} else {
			ctrl.Data["json"] = models.NewFailResponse(err)
		}
		ctrl.ServeJSON()
	} else {
		ctrl.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /104005 [post]
func (ctrl *WalletController) BindBankCard() {
	var w models.Wallet
	var rq models.BankCard
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &rq); err == nil {
		memberId := models.ID(ctrl.Ctx.Input.GetData("mbrId").(string))
		phone := ctrl.Ctx.Input.GetData("phone").(string)
		if cardId, err := w.BindBankCard(memberId, phone, rq); err == nil {
			ctrl.Data["json"] = models.NewOkResponse(cardId)
		} else {
			ctrl.Data["json"] = models.NewFailResponse(err)
		}

		ctrl.ServeJSON()
	} else {
		ctrl.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /104006 [post]
func (ctrl *WalletController) GetWalletDetail() {
	var w models.Wallet
	var rq models.PagedRQ
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &rq); err == nil {
		memberId := models.ID(ctrl.Ctx.Input.GetData("mbrId").(string))
		if rs, err := w.GetDetailList(memberId, rq.PageNo, rq.PageSize); err == nil {
			ctrl.Data["json"] = models.NewOkResponse(rs)
		} else {
			ctrl.Data["json"] = models.NewFailResponse(err)
		}

		ctrl.ServeJSON()
	} else {
		ctrl.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /104007 [post]
func (ctrl *WalletController) GetWithdrawDetailList() {
	var w models.Wallet
	var rq models.PagedRQ
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &rq); err == nil {
		memberId := models.ID(ctrl.Ctx.Input.GetData("mbrId").(string))
		if rs, err := w.GetWithdrawList(memberId, rq.PageNo, rq.PageSize); err == nil {
			ctrl.Data["json"] = models.NewOkResponse(rs)
		} else {
			ctrl.Data["json"] = models.NewFailResponse(err)
		}
		ctrl.ServeJSON()
	} else {
		ctrl.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /104008 [get]
func (ctrl *WalletController) GetBankCardList() {
	var w models.Wallet
	memberId := models.ID(ctrl.Ctx.Input.GetData("mbrId").(string))
	if rs, err := w.GetBankCardList(memberId); err == nil {
		ctrl.Data["json"] = models.NewOkResponse(rs)
	} else {
		ctrl.Data["json"] = models.NewFailResponse(err)
	}
	ctrl.ServeJSON()
}

// @router /104009 [post]
func (c *WalletController) BindQPayBankCard() {
	var w models.Wallet
	var b models.BankCard

	err := json.Unmarshal(c.Ctx.Input.RequestBody, &b)
	if err != nil {
		c.Ctx.ResponseWriter.WriteHeader(400)
		return
	}

	phone := c.Ctx.Input.GetData("phone").(string)
	memberID := models.ID(c.Ctx.Input.GetData("mbrId").(string))

	rs, err := w.BindQPayBankCard(memberID, phone, b)
	if err == nil {
		c.Data["json"] = models.NewOkResponse(rs)
	} else {
		c.Data["json"] = models.NewFailResponse(err)
	}
	c.ServeJSON()
}
